package com.dianning.service.imp;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import org.apache.commons.logging.*;

import com.dianning.bean.Company;
import com.dianning.bean.Money;
import com.dianning.bean.Profile;
import com.dianning.bean.Resume;
import com.dianning.bean.UserDetail;
import com.dianning.bean.UserInfo;
import com.dianning.bean.Visitor;
import com.dianning.dao.CompanyDAO;
import com.dianning.dao.MoneyDAO;
import com.dianning.dao.ProfileDAO;
import com.dianning.dao.ResumeDAO;
import com.dianning.dao.UserDetailDAO;
import com.dianning.dao.UserInfoDAO;
import com.dianning.dao.VisitorDAO;
import com.dianning.exception.DianningException;
import com.dianning.service.UserInfoService;
import com.dianning.service.VisitorService;

public class UserInfoServiceImp implements UserInfoService {

	private static final Log logger = LogFactory.getLog(UserInfoServiceImp.class);
	
	private UserInfoDAO userInfoDAO;
	private UserDetailDAO userDetailDAO;
	private ResumeDAO resumeDAO;
	private CompanyDAO companyDAO;
	private MoneyDAO moneyDAO;
	private ProfileDAO profileDAO;
	
	

	public UserInfoDAO getUserInfoDAO() {
		return userInfoDAO;
	}

	public void setUserInfoDAO(UserInfoDAO userInfoDAO) {
		this.userInfoDAO = userInfoDAO;
	}

	public UserDetailDAO getUserDetailDAO() {
		return userDetailDAO;
	}

	public void setUserDetailDAO(UserDetailDAO userDetailDAO) {
		this.userDetailDAO = userDetailDAO;
	}

	public ResumeDAO getResumeDAO() {
		return resumeDAO;
	}

	public void setResumeDAO(ResumeDAO resumeDAO) {
		this.resumeDAO = resumeDAO;
	}

	public CompanyDAO getCompanyDAO() {
		return companyDAO;
	}

	public void setCompanyDAO(CompanyDAO companyDAO) {
		this.companyDAO = companyDAO;
	}

	public MoneyDAO getMoneyDAO() {
		return moneyDAO;
	}

	public void setMoneyDAO(MoneyDAO moneyDAO) {
		this.moneyDAO = moneyDAO;
	}

	public ProfileDAO getProfileDAO() {
		return profileDAO;
	}

	public void setProfileDAO(ProfileDAO profileDAO) {
		this.profileDAO = profileDAO;
	}


	/**
	 * 保存UserInfo对象
	 * 
	 * @param userInfo
	 * 			UserInfo对象
	 * @return UserInfo
	 * 			UserInfo对象
	 */
	public UserInfo createUserInfo(UserInfo userInfo,UserDetail userDetail,Resume resume,Company company,Money money,Profile profile) throws DianningException{
		try {
			userInfo = this.getUserInfoDAO().saveUserInfo(userInfo);
			userDetail.setUid(userInfo.getId());
			this.getUserDetailDAO().saveUserDetail(userDetail);
			resume.setUid(userInfo.getId());
			this.getResumeDAO().saveResume(resume);
			company.setUid(userInfo.getId());
			this.getCompanyDAO().saveCompany(company);
			money.setUid(userInfo.getId());
			this.getMoneyDAO().saveMoney(money);
			profile.setUid(userInfo.getId());
			this.getProfileDAO().saveProfile(profile);
			return userInfo;
		} catch (Exception e) {
			logger.error(e);
			throw new DianningException(e);
		}
	}
	
	/**
	 * 根据主键ID获取UserInfo
	 * @param uid
	 * 			主键ID
	 * @return UserInfo
	 * 			UserInfo对象
	 */
	public UserInfo findUserInfoByUid(long uid){
		UserInfo ui = this.getUserInfoDAO().findUserInfoById(uid);
		UserDetail ud = this.getUserDetailDAO().findUserDetailByUid(uid);
		ui.setUserDetail(ud);
		Resume resume = this.getResumeDAO().findResumeByUId(uid);
		ui.setResume(resume);
		Money money = this.getMoneyDAO().findMoneyByUid(uid);
		ui.setMoney(money);
		return ui;
	}
	
	/**
	 * 根据username获取UserInfo对象
	 * 
	 * @param username
	 * 			String
	 * @return UserInfo
	 * 			UserInfo对象
	 */
	public UserInfo findUserInfoByUsername(String username){
		return this.getUserInfoDAO().findUserInfoByUsername(username);
	}
	
	/**
	 * 更新UserInfo对象
	 * @param userInfo
	 * 			UserInfo对象
	 * @return UserInfo
	 * 			UserInfo对象
	 */
	public UserInfo updateUserInfo(UserInfo userInfo){
		if(userInfo.getUserDetail() != null){
			this.getUserDetailDAO().updateUserDetail(userInfo.getUserDetail());
		}
		if(userInfo.getResume() != null){
			this.getResumeDAO().updateResume(userInfo.getResume());
		}
		return this.getUserInfoDAO().updateUserInfo(userInfo);
	}
	
	/**
	 * 根据主键ID获取Resume对象
	 * 
	 * @param id
	 * 			主键ID
	 * @return Resume
	 * 			Resume对象
	 */
	public Resume findResumeByUId(long uid){
		return this.getResumeDAO().findResumeByUId(uid);
	}
	/**
	 * 保存Resume对象
	 * 
	 * @param resume
	 * 			Resume对象
	 * @return Resume
	 * 			Resume对象
	 */
	public Resume updateResume(Resume resume){
		return this.getResumeDAO().updateResume(resume);
	}
	
	/**
	 * 保存UserDetail对象
	 * 
	 * @param userDetail
	 * 			UserDetail对象
	 * @return UserDetail
	 * 			UserDetail对象
	 */
	public UserDetail updateUserDetail(UserDetail userDetail){
		return this.getUserDetailDAO().updateUserDetail(userDetail);
	}
	
	/**
	 * 根据主键ID获取UserDetail对象
	 * 
	 * @param iid
	 * 			主键ID
	 * @return UserDetail
	 * 			UserDetail对象
	 */
	public UserDetail findUserDetailByUid(long uid){
		return this.getUserDetailDAO().findUserDetailByUid(uid);
	}
	
	/**
	 * 根据主键ID获取Company对象
	 * 
	 * @param uid
	 * 			主键ID
	 * @return Company
	 * 			Company对象
	 */
	public Company findCompanyByUid(long uid){
		return this.getCompanyDAO().findCompanyByUid(uid);
	}
	/**
	 * 保存Company对象
	 * 
	 * @param company
	 * 			Company对象
	 * @return Company
	 * 			Company对象
	 */
	public Company updateCompany(Company company){
		return this.getCompanyDAO().updateCompany(company);
	}
	
}
